package com.zhangmeng.tools.controller;

import com.zhangmeng.tools.components.RxEditer;
import com.zhangmeng.tools.dto.FileTreeView;
import com.zhangmeng.tools.editors.ace.AceEditor;
import com.zhangmeng.tools.editors.ace.AceMode;
import com.zhangmeng.tools.editors.ace.AceTheme;
import com.zhangmeng.tools.editors.codemirr.CodeMirrWebView;
import com.zhangmeng.tools.editors.monaco.*;
import com.zhangmeng.tools.editors.timifx.TimiFxEditor;
import com.zhangmeng.tools.utils.*;
import javafx.application.Platform;
import javafx.beans.property.SimpleDoubleProperty;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.property.SimpleObjectProperty;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.embed.swing.SwingFXUtils;
import javafx.embed.swing.SwingNode;
import javafx.event.ActionEvent;
import javafx.event.Event;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.geometry.Pos;
import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.*;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.HBox;
import javafx.scene.paint.Paint;
import javafx.scene.text.Font;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebView;
import javafx.stage.DirectoryChooser;
import javafx.stage.FileChooser;
import javafx.stage.Stage;
import javafx.util.Callback;
import lombok.extern.slf4j.Slf4j;
import net.imyeyu.timifx.service.RunAsync;
import net.imyeyu.timijava.config.Config;
import net.imyeyu.timijava.config.ConfigManager;
import org.apache.commons.io.FilenameUtils;
import org.fife.ui.rsyntaxtextarea.SyntaxConstants;
import org.fxmisc.flowless.VirtualizedScrollPane;
import org.fxmisc.richtext.CodeArea;

import javax.swing.*;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.text.SimpleDateFormat;
import java.util.*;

import static net.imyeyu.timifx.TimiFX.BORDER_EXLEFT;

@Slf4j
public class EditorListImplController {

    private final SimpleIntegerProperty editer_index = new SimpleIntegerProperty();

    @FXML
    public MenuItem open_file;

    @FXML
    public MenuItem open_dir;

    @FXML
    public MenuItem save;

    @FXML
    public Menu them_list;

    @FXML
    public MenuItem new_file;

    @FXML
    public Menu font_item;


    public enum Type {
        JAVA, HTML, GO, JS, CSS, SQL, XML, YML, MARKDOWN,PROPERTIES,JSON
    }

    private TabPane tabPane = null;
    private SimpleDoubleProperty width = new SimpleDoubleProperty(0.0);
    private SimpleDoubleProperty height = new SimpleDoubleProperty(0.0);
    private AnchorPane root;

    private AceEditor acJsEditor;

    private MonacoWebView monacoEdit;
    private CodeMirrWebView codemirror;
    private AnchorPane timiFx;

    private SimpleObjectProperty<File> choose_file = new SimpleObjectProperty<>(null);
    private SimpleObjectProperty<AceTheme> choose_them = new SimpleObjectProperty<>(null);
    private final ObservableList<Path> filePaths_list = FXCollections.observableArrayList();
    public static final String color_cell = "#f4f4f4";

    public static final Set<Tab> remove_set = new HashSet<>();

    @FXML
    private ListView<ResourcesUtils.EditorList> listView;

    @FXML
    private SplitPane splitPane;

    private static final Map<Path, Tab> map = new HashMap<>();

    @FXML
    public void date_query_menu_item() {
        load_small_tools(5);
    }

    @FXML
    public void cron_menu_item() {
        load_small_tools(6);
    }

    @FXML
    public void video_menu_item() {
        load_player(0);
    }

    @FXML
    public void music_menu_item() {
        load_player(1);
    }

    @FXML
    public void vip_parser_menu_item() {
        load_player(2);
    }

    @FXML
    public void hex_16() {
        load_small_tools(0);
    }

    @FXML
    private void hex_16_menu_item() {
        load_small_tools(0);
    }

    @FXML
    private void unicode_menu_item() {
        load_small_tools(1);
    }

    @FXML
    private void jwt_menu_item() {
        load_small_tools(2);
    }

    @FXML
    private void color_choose_menu_item() {
        load_small_tools(3);
    }

    @FXML
    public void qr_code_menu_item() {
        load_small_tools(4);
    }

    @FXML
    public void base_62_menu_item() {
        load_codec_tools(0);
    }

    @FXML
    public void base_64_menu_item() {
        load_codec_tools(1);
    }

    @FXML
    public void base_32_menu_item() {
        load_codec_tools(2);
    }

    @FXML
    public void morse_coder_menu_item() {
        load_codec_tools(3);
    }

    @FXML
    private void sql_code_gen_menu_item() {
        load_sql_tools(0);
    }

    @FXML
    public void netty_client_menu_item() {
        load_network_tools(0);
    }

    public void load_network_tools(int index) {
        AnchorPane fx = null;
        try {
            fx = FXMLLoader.load(ResourcesUtils.getResource("network-tools"));
        } catch (IOException e) {
            e.printStackTrace();
        }

        Scene scene = new Scene(fx);
        Stage stage = (Stage) splitPane.getScene().getWindow();
        stage.setScene(scene);

        ListView<ResourcesUtils.Player> listView = (ListView) fx.lookup("#listView");
        listView.getSelectionModel().select(index);
    }

    public void load_sql_tools(int index) {
        AnchorPane fx = null;
        try {
            fx = FXMLLoader.load(ResourcesUtils.getResource("sql-tools"));
        } catch (IOException e) {
            e.printStackTrace();
        }

        Scene scene = new Scene(fx);
        Stage stage = (Stage) splitPane.getScene().getWindow();
        stage.setScene(scene);

        ListView<ResourcesUtils.Player> listView = (ListView) fx.lookup("#listView");
        listView.getSelectionModel().select(index);
    }

    public void load_codec_tools(int index) {
        AnchorPane fx = null;
        try {
            fx = FXMLLoader.load(ResourcesUtils.getResource("codec-tools"));
        } catch (IOException e) {
            e.printStackTrace();
        }

        Scene scene = new Scene(fx);
        Stage stage = (Stage) splitPane.getScene().getWindow();
        stage.setScene(scene);

        ListView<ResourcesUtils.Player> listView = (ListView) fx.lookup("#listView");
        listView.getSelectionModel().select(index);
    }

    public void load_small_tools(int index) {
        AnchorPane fx = null;
        try {
            fx = FXMLLoader.load(ResourcesUtils.getResource("small-tools"));
        } catch (IOException e) {
            e.printStackTrace();
        }

        Scene scene = new Scene(fx);
        Stage stage = (Stage) splitPane.getScene().getWindow();
        stage.setScene(scene);

        ListView<ResourcesUtils.Player> listView = (ListView) fx.lookup("#listView");
        listView.getSelectionModel().select(index);
    }

    public void load_player(int index) {
        AnchorPane fx = null;
        try {
            fx = FXMLLoader.load(ResourcesUtils.getResource("player"));
        } catch (IOException e) {
            e.printStackTrace();
        }

        Scene scene = new Scene(fx);
        Stage stage = (Stage) splitPane.getScene().getWindow();
        stage.setScene(scene);

        ListView<ResourcesUtils.Player> listView = (ListView) fx.lookup("#listView");
        listView.getSelectionModel().select(index);
    }

    @FXML
    public void initialize() {
        String local = ConfigManager.getConfig(Resources.CONFIG_PATH).getString(Config.section("Main").key("AceJsThem"));
        choose_them.setValue(AceTheme.valueOf(local));
        init();
        listView.getSelectionModel().selectedItemProperty().addListener((observable, oldValue, newValue) -> {
            if (newValue != null) {
                boolean flag = false;

                if (newValue.getIndex() == 0) {
                    if (acJsEditor != null) {
                        flag = true;
                    }
                    acJsEditor(flag);
                }

                if (newValue.getIndex() == 1) {
                    if (monacoEdit != null) {
                        flag = true;
                    }
                    monacoEdit(flag);
                }

                if (newValue.getIndex() == 2) {
                    if (codemirror != null) {
                        flag = true;
                    }
                    codemirror(flag);
                }

                if (newValue.getIndex() == 3) {
                    if (timiFx != null) {
                        flag = true;
                    }
                    timiFx(flag);
                }
            }
        });
    }

    public static Image getImage(ResourcesUtils.EditorList player) {
        return switch (player) {
            case Ace_JS -> new Image(ImagePath.path(ImagePath.ImagePathType.MD5));
            case Monaco_JS -> new Image(ImagePath.path(ImagePath.ImagePathType.MD5));
            case CodeMirror_JS -> new Image(ImagePath.path(ImagePath.ImagePathType.MD5));
            case Timi_Fx -> new Image(ImagePath.path(ImagePath.ImagePathType.MD5));
            case Swing -> new Image(ImagePath.path(ImagePath.ImagePathType.MD5));
        };
    }

    public void init() {
        ResourcesUtils.EditorList[] values = ResourcesUtils.EditorList.values();
        ObservableList<ResourcesUtils.EditorList> list = FXCollections.observableArrayList();
        list.addAll(Arrays.asList(values));
        listView.setItems(list);
        listView.setFixedCellSize(40);
        listView.setCellFactory(new Callback<>() {
            private int position;

            @Override
            public ListCell<ResourcesUtils.EditorList> call(ListView<ResourcesUtils.EditorList> playerListView) {
                Label label = new Label();
                label.setPrefWidth(200);
                ListCell<ResourcesUtils.EditorList> listCell = new ListCell<>() {
                    @Override
                    protected void updateItem(ResourcesUtils.EditorList player, boolean b) {
                        super.updateItem(player, b);
                        if (!b) {
                            HBox hBox = new HBox(25);
                            hBox.setAlignment(Pos.CENTER);
                            label.setText(player.getTitle());
                            label.setTextFill(Paint.valueOf("#000000"));
                            Image im = getImage(player);
                            ImageView iv = new ImageView(im);
                            iv.setPreserveRatio(true);
                            iv.setFitWidth(15);
                            hBox.getChildren().add(iv);

                            hBox.getChildren().add(label);
                            this.setGraphic(hBox);
                        }
                        this.setStyle("-fx-background-color: " + color_cell);
                    }
                };

                listCell.hoverProperty().addListener((observableValue, aBoolean, t1) -> {
                    if (t1 && !label.getText().equals("")) {
                        position = playerListView.getItems().indexOf(label.getText());
                        label.setFont(new Font(16));
                        playerListView.getFocusModel().focus(position);
                        listCell.setStyle("-fx-background-color: #369e7d");
                    } else {
                        label.setPrefHeight(20);
                        label.setFont(new Font(13));
                        listCell.setStyle("-fx-background-color: " + color_cell);
                    }
                });

                return listCell;
            }
        });
        tabPane = new TabPane();
        //添加菜单
        addMenu();
        load_them_edit();
        File file = null;
        FileTreeView fileTreeView = new FileTreeView(file);
        fileTreeView.setBorder(BORDER_EXLEFT);
        splitPane.getItems().clear();
        splitPane.getItems().add(0, fileTreeView);
        splitPane.getItems().add(1, tabPane);
        splitPane.setDividerPosition(0, 0.20);
        splitPane.setDividerPosition(1, 0.80);
    }

    public void addMenu() {

        // 设置快捷键
        save.setAccelerator(new KeyCodeCombination(KeyCode.S, KeyCodeCombination.CONTROL_DOWN));
        save.setOnAction(event -> writer_file());

        open_file.setAccelerator(new KeyCodeCombination(KeyCode.F, KeyCodeCombination.CONTROL_DOWN));
        open_file.setOnAction(event -> open_file());

        open_dir.setAccelerator(new KeyCodeCombination(KeyCode.D, KeyCodeCombination.CONTROL_DOWN));
        open_dir.setOnAction(event -> open_file_dir());

        new_file.setAccelerator(new KeyCodeCombination(KeyCode.N, KeyCodeCombination.CONTROL_DOWN));
        new_file.setOnAction(event -> new_file());

    }

    public void load_them_edit(){
        them_list.getItems().clear();
        if (editer_index.getValue()==null || editer_index.getValue() == 0){

            ListView<String> listView = new ListView<>();

            ObservableList<String> objects = FXCollections.observableArrayList();
            for (AceTheme value : AceTheme.values()) {
                String them_string = value.toString();
                objects.add(them_string);
            }
            listView.setItems(objects);

            listView.getSelectionModel().selectedItemProperty().addListener(new ChangeListener<String>() {
                @Override
                public void changed(ObservableValue<? extends String> observable, String oldValue, String newValue) {
                    if (newValue!= null){
                        if (tabPane.getSelectionModel().getSelectedItem() == null){
                            choose_them.setValue(AceTheme.valueOf(newValue));
                            AlertUtils.alert_warning("主题选择成功!");
                            return;
                        }
                        AceEditor aceEditor = (AceEditor) tabPane.getSelectionModel().getSelectedItem().getContent();
                        aceEditor.setAceTheme(AceTheme.valueOf(newValue));
                    }
                }
            });
            MenuItem menuItem = new MenuItem("设置主题");
            them_list.getItems().add(menuItem);
            menuItem.setOnAction(event -> {
                AnchorPane them_choose = new AnchorPane();
                them_choose.setPrefWidth(200);
                them_choose.setPrefHeight(400);
                them_choose.getChildren().add(listView);
                Stage primaryStage = (Stage) EditorListImplController.this.splitPane.getScene().getWindow();
                AlertUtils.alert("设置", them_choose,200,400, primaryStage);
            });
        }

        if (editer_index.getValue() == 1){

            ListView<String> listView = new ListView<>();

            ObservableList<String> objects = FXCollections.observableArrayList();
            objects.add("dark");
            objects.add("default");
            objects.add("default-alt");
            objects.add("druid");
            objects.add("eclipse");
            objects.add("idea");
            objects.add("monokai");
            objects.add("theme");
            objects.add("vs");
            listView.setItems(objects);

            listView.getSelectionModel().selectedItemProperty().addListener((observable, oldValue, newValue) -> {
                if (newValue!= null){
                    if (tabPane.getSelectionModel().getSelectedItem() == null){
                        //choose_them.setValue(newValue);
                        AlertUtils.alert_warning("主题选择成功!");
                        return;
                    }
                    RxEditer aceEditor = (RxEditer) tabPane.getSelectionModel().getSelectedItem().getContent();
                    aceEditor.setTheme(newValue);
                }
            });

            MenuItem menuItem = new MenuItem("设置主题");
            them_list.getItems().add(menuItem);
            menuItem.setOnAction(event -> {
                AnchorPane them_choose = new AnchorPane();
                them_choose.setPrefWidth(200);
                them_choose.setPrefHeight(400);
                them_choose.getChildren().add(listView);
                Stage primaryStage = (Stage) EditorListImplController.this.splitPane.getScene().getWindow();
                AlertUtils.alert("设置", them_choose,200,400, primaryStage);
            });
        }
    }

    //
    public void new_file(){

    }

    public String file_name_gen(){
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss");
       return simpleDateFormat.format(new Date()) + ".txt";
    }

    public void open_file() {
        Stage stage = new Stage();
        FileChooser dc = new FileChooser();
        dc.setTitle("文件选择");
        dc.getExtensionFilters().addAll(new FileChooser.ExtensionFilter("文件类型", "*.*"));
        File file = dc.showOpenDialog(stage);
        if (file != null) {
            refresh(file, 0);
        }
    }

    public void open_file_dir() {

        Stage stage = new Stage();
        DirectoryChooser dc = new DirectoryChooser();
        dc.setTitle("文件夹选择");
        File file = dc.showDialog(stage);
        if (file != null) {
            if (file.isDirectory()) {
                refresh(file, 1);
            }
        }
    }

    public void refresh(File file, int type) {
        file_checked(file, type);
    }

    private void acJsEditor(boolean flag) {
        //默认选择第一个
        listView.getSelectionModel().select(0);

        if (!flag) {
            acJsEditor = new AceEditor();
            root = acJsEditor;
            WebView view = acJsEditor.getWebView();
            AnchorPane.setTopAnchor(view, 0.0);
            AnchorPane.setBottomAnchor(view, 0.0);
            AnchorPane.setLeftAnchor(view, 0.0);
            AnchorPane.setRightAnchor(view, 0.0);

        } else {
            root = acJsEditor;
        }
        common_method();
    }

    private void monacoEdit(boolean flag) {

        //默认选择第一个
        listView.getSelectionModel().select(1);
        if (!flag) {
            monacoEdit = new MonacoWebView();
            root = monacoEdit;
        } else {
            root = monacoEdit;
        }
        common_method();
    }

    private void codemirror(boolean flag) {

        //默认选择第一个
        listView.getSelectionModel().select(2);
        if (!flag) {
            codemirror = new CodeMirrWebView();
            root = codemirror;
        } else {
            root = codemirror;
        }
        common_method();
    }

    private void timiFx(boolean flag) {

        //默认选择第一个
        listView.getSelectionModel().select(3);
        if (!flag) {
            root = new TimiFxEditor();
            timiFx = root;
        } else {
            root = timiFx;
        }
        common_method();
    }

    public void writer_file() {

        //获取所选table
        Tab tab = this.tabPane.getSelectionModel().getSelectedItem();
        AceEditor aceEditor = (AceEditor) tab.getContent();
        log.info("aceEditor:" + aceEditor.getFile().getName());

        String text = aceEditor.getText();

        try {
            FileWriter writer = new FileWriter(choose_file.get());
            writer.write(text);
            writer.flush();
            writer.close();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        AlertUtils.alert_warning("tab " + choose_file.get().getName() + "保存成功!");
    }

    public void file_checked(File file, int open_type) {
        String extension = FilenameUtils.getExtension(file.getName());
        boolean flag = true;

        if (extension.equals("mp4")) {
            flag = false;
        }

        if (extension.equals("jpg")) {
            flag = false;
        }

        if (extension.equals("png")) {
            flag = false;
        }

        if (extension.equals("avi")) {
            flag = false;
        }

        if (extension.equals("zip")) {
            flag = false;
        }

        if (extension.equals("ico")) {
            flag = false;
        }

        if (extension.equals("exe")) {
            flag = false;
        }
        if (flag) {
            Type type = null;

            if (extension.equals("java")) {
                type = Type.JAVA;
            }

            if (extension.equals("go")) {
                type = Type.GO;
            }
            if (extension.equals("html")) {
                type = Type.HTML;
            }
            if (extension.equals("sql")) {
                type = Type.SQL;
            }
            if (extension.equals("xml")) {
                type = Type.XML;
            }
            if (extension.equals("yml")) {
                type = Type.YML;
            }

            if (extension.equals("md")) {
                type = Type.MARKDOWN;
            }

            if (extension.equals("properties")) {
                type = Type.PROPERTIES;
            }

            if (type == null) {
                type = Type.JAVA;
            }

            open_file(file, type, open_type);
        } else {
            AlertUtils.alert_warning("该文件不支持!");
        }
    }

    public String read_content(File file){

        String file_content = null;
        try {
            file_content = Files.readString(file.toPath(), StandardCharsets.UTF_8);
        } catch (IOException e) {
            try {
                file_content = Files.readString(file.toPath(), Charset.forName("GBK"));
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
        return file_content;
    }

    public void open_file(File file, Type type, int open_type) {

        //打开文件
        //打开文件夹
        choose_file.setValue(file);

        if (open_type == 1) {
            common_method();
        }

        Path path = file.toPath();
        if (filePaths_list.contains(path)) {//size -1 ,szie -2
            //切换到一打开的tab
            Tab tab = map.get(path);
            if (!tabPane.getTabs().contains(tab)) {
                if (remove_set.contains(tab)){
                    //重新加载内容
                    if (editer_index.getValue() == 0){
                        AceEditor aceEditor = (AceEditor) tab.getContent();
                        aceEditor.setAceText(read_content(file));
                        tab.setContent(aceEditor);
                    }
                    if (editer_index.getValue() == 1){
                        RxEditer sw = (RxEditer) tab.getContent();
                        sw.setText(read_content(file));
                        tab.setContent(sw);
                    }
                }

                tabPane.getTabs().add(tab);
            }
            tabPane.getSelectionModel().select(tab);
        } else {
            if (choose_file.get().isFile()) {

                //异步加载
                RunAsync.runback(() -> {
                    return read_content(choose_file.getValue());
                }, file_content -> {

                    Tab tab = new Tab(file.getName());
                    ContextMenu contextMenu = new ContextMenu();
                    MenuItem close = new MenuItem("close");
                    MenuItem close_other_tabs = new MenuItem("close other tabs");
                    MenuItem close_all = new MenuItem("close all");
                    close_all.setOnAction(event -> {
                        for (Tab tabPaneTab : tabPane.getTabs()) {
                            remove_set.add(tabPaneTab);
                        }
                        tabPane.getTabs().clear();

                    });
                    close_other_tabs.setOnAction(event -> {
                        ObservableList<Tab> tabs = tabPane.getTabs();
                        ObservableList<Tab> list = FXCollections.observableArrayList(tabs);
                        //关闭其他
                        for (Tab tabPaneTab : list) {
                            if (tabPaneTab != tabPane.getSelectionModel().getSelectedItem()) {
                                tabs.remove(tabPaneTab);
                                remove_set.add(tabPaneTab);
                            }
                        }
                    });
                    close.setOnAction(event -> {
                        //关闭当前窗口
                        Tab selectedItem = tabPane.getSelectionModel().getSelectedItem();
                        tabPane.getTabs().remove(selectedItem);
                        remove_set.add(selectedItem);
                    });
                    contextMenu.getItems().add(close);
                    contextMenu.getItems().add(close_other_tabs);
                    contextMenu.getItems().add(close_all);


                    tab.setContextMenu(contextMenu);
                    tab.setOnCloseRequest(new EventHandler<Event>() {
                        @Override
                        public void handle(Event event) {
                            //                    log.info("tabtab.setOnCloseRequest:--->" + tab.getText());
                            tabPane.getTabs().remove(tab);
                            remove_set.add(tab);
                        }
                    });

                    //判断是哪个编辑器
                    if (editer_index.getValue() == 0){
                        AceMode mode = null;
                        switch (type) {
                            case JAVA -> mode = AceMode.JAVA;
                            case HTML -> mode = AceMode.HTML;
                            case XML -> mode = AceMode.XML;
                            case SQL -> mode = AceMode.SQL;
                            case MARKDOWN -> mode = AceMode.MARKDOWN;
                            case PROPERTIES -> mode = AceMode.PROPERTIES;
                            case JSON -> mode = AceMode.JSON;
                        }
                        //新建编辑器
                        AceEditor editor = new AceEditor(choose_file.getValue(), mode, EditorListImplController.this.choose_them.get());
                        WebView view = editor.getWebView();
                        AnchorPane.setTopAnchor(view, 0.0);
                        AnchorPane.setBottomAnchor(view, 0.0);
                        AnchorPane.setLeftAnchor(view, 0.0);
                        AnchorPane.setRightAnchor(view, 0.0);
                        tab.setContent(editor);
                    }
                    if (editer_index.getValue() == 1){
                        String style = null;
                        switch (type) {
                            case JAVA -> style = SyntaxConstants.SYNTAX_STYLE_JAVA;
                            case HTML -> style = SyntaxConstants.SYNTAX_STYLE_HTML;
                            case XML -> style = SyntaxConstants.SYNTAX_STYLE_XML;
                            case SQL -> style = SyntaxConstants.SYNTAX_STYLE_SQL;
                            case MARKDOWN -> style = SyntaxConstants.SYNTAX_STYLE_MARKDOWN;
                        }
                        RxEditer rx = new RxEditer(style,"idea");
//                        rx.setTheme("idea");
                        rx.setText(file_content);
                        tab.setContent(rx);
                    }

                    tabPane.getTabs().add(tab);
                    tabPane.getSelectionModel().select(tab);

                    filePaths_list.add(path);
                    map.put(path, tab);
                });
            }
        }
    }

    /**
     * File file = new File("D:\\generate");
     */
    private void common_method() {
        //splitPane.getItems().remove(1);
        File file = choose_file.get();

        //添加菜单
        splitPane.getItems().clear();

        //添加文件选择
        FileTreeView fileTreeView = new FileTreeView(file);
        fileTreeView.setBorder(BORDER_EXLEFT);
        //fileTreeView.addItemFilter(file -> file.isDirectory() || file.getName().endsWith(".txt"));
        splitPane.getItems().add(0, fileTreeView);
        splitPane.getItems().add(1, tabPane);
        splitPane.setDividerPosition(0, 0.2);
        splitPane.setDividerPosition(1, 0.8);

        fileTreeView.getSelectionModel().selectedItemProperty().addListener(new ChangeListener<TreeItem<File>>() {
            @Override
            public void changed(ObservableValue<? extends TreeItem<File>> observable, TreeItem<File> oldValue, TreeItem<File> newValue) {
                if (newValue != null) {
                    File value = newValue.getValue();
                    if (!value.isDirectory()) {
                        choose_file.setValue(value);
                        log.info("selectedItemProperty:------------>:" + value.getName());
                    }
                }
            }

        });


        fileTreeView.setOnMouseClicked(event -> {
            if (event.getButton() == MouseButton.PRIMARY && event.getClickCount() == 2) {
                // 双击左键的操作
                log.info("open file:" + choose_file.get().getName());
                if (choose_file.getValue() != null) {
                    file_checked(choose_file.get(), 0);
                }
            }
        });

//        root.widthProperty().addListener((observable, oldValue, newValue) -> {
//            if (newValue != null) {
//                double width = splitPane.getWidth();
//                EditorListImplController.this.width.set(width);
//                log.info("home:--->width:{}", width);
//            }
//        });
//        root.heightProperty().addListener((observable, oldValue, newValue) -> {
//            if (newValue != null) {
//                double height = splitPane.getHeight();
//                EditorListImplController.this.height.set(height);
//                log.info("home:--->height:{}", height);
//            }
//        });
//
//        this.width.addListener((observable, oldValue, newValue) -> {
//            EditorListImplController.this.root.setPrefWidth(newValue.doubleValue() - listView.getWidth());
//            log.info("root:=====================>width:" + (newValue.doubleValue() - listView.getWidth()));
//        });
//
//        this.height.addListener((observable, oldValue, newValue) -> {
//            EditorListImplController.this.root.setPrefHeight(newValue.doubleValue() - listView.getHeight());
//            log.info("root:=====================>:height" + (newValue.doubleValue() - listView.getWidth()));
//        });
    }

    @FXML
    public void http_request_menu_item(ActionEvent event) {
        load_http_tools(0);
    }

    @FXML
    public void http_upload_menu_item(ActionEvent event) {
        load_http_tools(1);
    }

    @FXML
    public void http_download_menu_item(ActionEvent event) {
        load_http_tools(2);
    }

    public void load_http_tools(int index) {
        AnchorPane fx = null;
        try {
            fx = FXMLLoader.load(ResourcesUtils.getResource("http-tools"));
        } catch (IOException e) {
            e.printStackTrace();
        }

        Scene scene = new Scene(fx);
        Stage stage = (Stage) splitPane.getScene().getWindow();
        stage.setScene(scene);

        ListView<ResourcesUtils.Player> listView = (ListView) fx.lookup("#listView");
        listView.getSelectionModel().select(index);
    }

    @FXML
    public void mail_menu_item(ActionEvent event) {
        load_small_tools(7);
    }

    public void http_server_menu_item(ActionEvent event) {
        load_server_tools(0);
    }

    public void ftp_server_menu_item(ActionEvent event) {
        load_server_tools(1);
    }

    public void load_server_tools(int index) {
        AnchorPane fx = null;
        try {
            fx = FXMLLoader.load(ResourcesUtils.getResource("server-tools"));
        } catch (IOException e) {
            e.printStackTrace();
        }

        Scene scene = new Scene(fx);
        Stage stage = (Stage) splitPane.getScene().getWindow();
        stage.setScene(scene);

        ListView<ResourcesUtils.Player> listView = (ListView) fx.lookup("#listView");
        listView.getSelectionModel().select(index);
    }

    public void ssh_client_menu_item(ActionEvent event) {
        load_server_tools(2);
    }

    public void socket_server_menu_item(ActionEvent event) {
        load_server_tools(3);
    }

    public void telephone_menu_item(ActionEvent event) {
        load_small_tools(8);
    }

    public void mybatis_plus_gen_menu_item(ActionEvent event) {
        load_sql_tools(1);
    }

    public void JsonView_menu_item(ActionEvent event) {

        load_small_tools(9);
    }

    public void maven_jar_install_menu_item(ActionEvent event) {
        load_small_tools(10);
    }

    public void word_ocr_menu_item(ActionEvent event) {
        load_small_tools(11);
    }

    public void bar_code_menu_item(ActionEvent event) {
        load_small_tools(12);
    }

    public void pdf_menu_item(ActionEvent event) {
        load_small_tools(13);
    }

    public void json_javabean_gen_menu_item(ActionEvent event) {
        load_sql_tools(2);
    }

    public void music_parser_menu_item(ActionEvent event) {
        load_player(3);
    }

    public void batch_update_file_name_menu_item(ActionEvent event) {
        load_small_tools(14);
    }

    public void socket_client_aio_menu_item(ActionEvent event) {
        load_server_tools(4);
    }

    public void socket_server_aio_menu_item(ActionEvent event) {
        load_server_tools(5);
    }

    public void socket_server_nio_menu_item(ActionEvent event) {
        load_server_tools(6);
    }

    public void socket_client_nio_menu_item(ActionEvent event) {
        load_server_tools(7);
    }

    public void capter_screen_menu_item(ActionEvent event) {
        load_small_tools(15);
    }

    public void sql_query_gen_menu_item(ActionEvent event) {
        load_sql_tools(3);
    }

    public void video_transcoder_menu_item(ActionEvent event) {
        load_player(4);
    }

    public void json_to_struct_menu_item(ActionEvent event) {
        load_go_tools(0);
    }


    public void mysql_to_struct_menu_item(ActionEvent actionEvent) {
        load_go_tools(1);
    }

    public void load_go_tools(int index) {
        AnchorPane fx = null;
        try {
            fx = FXMLLoader.load(ResourcesUtils.getResource("go-tools"));
        } catch (IOException e) {
            e.printStackTrace();
        }

        Scene scene = new Scene(fx);
        Stage stage = (Stage) splitPane.getScene().getWindow();
        stage.setScene(scene);

        ListView<ResourcesUtils.Player> listView = (ListView) fx.lookup("#listView");
        listView.getSelectionModel().select(index);
    }

    public void file_edit_menu_item(ActionEvent event) {
        load_small_tools(16);
    }

    public void web_socket_client_menu_item(ActionEvent event) {
        load_server_tools(8);
    }

    public void layui_form_gen_menu_item(ActionEvent actionEvent) {
        load_small_tools(17);
    }

    public void log_console_menu_item(ActionEvent actionEvent) {
        load_small_tools(18);
    }

    public void edit_plus_code_menu_item(ActionEvent actionEvent) {
        load_small_tools(19);
    }

    public void minio_upload_menu_item(ActionEvent actionEvent) {
        load_small_tools(20);
    }

    public void excel_read_menu_item(ActionEvent actionEvent){
        load_small_tools(21);
    }


    public void js_edit_list(int index) {
        AnchorPane fx = null;
        try {
            fx = FXMLLoader.load(ResourcesUtils.getResource("editor-list"));
        } catch (IOException e) {
            e.printStackTrace();
        }

        Scene scene = new Scene(fx);
        Stage stage = (Stage) splitPane.getScene().getWindow();
        stage.setScene(scene);

        ListView<ResourcesUtils.EditorList> listView = (ListView) fx.lookup("#listView");
        listView.getSelectionModel().select(index);
    }

    public void md5_menu_item(ActionEvent actionEvent) {

        load_encrypt(0);
    }

    public void spring_security_menu_item(ActionEvent actionEvent) {
        load_encrypt(1);
    }

    public void jks_file_menu_item(ActionEvent actionEvent) {
        load_encrypt(2);
    }

    public void load_encrypt(int index) {

        Stage stage = (Stage) splitPane.getScene().getWindow();

        AnchorPane fx = null;
        try {
            fx = FXMLLoader.load(ResourcesUtils.getResource("home"));
        } catch (IOException e) {
            e.printStackTrace();
        }
        Scene scene = new Scene(fx);
        stage.setScene(scene);

        ListView<ResourcesUtils.Player> listView = (ListView) fx.lookup("#listView");
        listView.getSelectionModel().select(index);
    }

    public void ace_js_menu_item(ActionEvent actionEvent) {
        js_edit_list(0);
    }

    public void monaco_js_menu_item(ActionEvent actionEvent) {
        js_edit_list(1);
    }

    public void codemirror_js_menu_item(ActionEvent actionEvent) {
        js_edit_list(2);
    }

    public void timeFx_fx_menu_item(ActionEvent actionEvent) {
        js_edit_list(3);
    }

    public void ace_menu_item(ActionEvent actionEvent) {
        editer_index.setValue(0);
        boolean flag = false;
        if (acJsEditor != null) {
            flag = true;
        }
        load_them_edit();
        acJsEditor(flag);
    }

    public void chat_app_menu_item(ActionEvent actionEvent) {
        load_chat_app(0);
    }

    public void load_chat_app(int index){
        AnchorPane fx = null;
        try {
            fx = FXMLLoader.load(ResourcesUtils.getResource("chat-app"));
        } catch (IOException e) {
            e.printStackTrace();
        }

        Scene scene = new Scene(fx);
        Stage stage = (Stage) splitPane.getScene().getWindow();
        stage.setScene(scene);

        ListView<ResourcesUtils.Player> listView = (ListView) fx.lookup("#listView");
        listView.getSelectionModel().select(index);
    }

    public void swing_menu_item(ActionEvent actionEvent) {
        js_edit_list(4);
    }
}
